package com.mk.episodecalendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.style.StyleSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LayoutObjectFactory 
{
	public View createEpisodeItem(final Context context, final Episode episode, Boolean hasAired)
	{
		//Episode container
		RelativeLayout container = new RelativeLayout(context);
		container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		container.setPadding(10, 5, 0, 5);
		
		//Text container
		LinearLayout textContainer = new LinearLayout(context);
		textContainer.setOrientation(LinearLayout.VERTICAL);
		textContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
		container.addView(textContainer);
		
		//Show name text
		TextView showView = new TextView(context);
		showView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	showView.setText(episode.getShow() + " - " + episode.getNumber(), TextView.BufferType.SPANNABLE);
    	Spannable showViewSpannable = (Spannable) showView.getText();
    	showViewSpannable.setSpan(new StyleSpan(Typeface.BOLD), 0, episode.getShow().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    	textContainer.addView(showView);
    	
    	//Episode title text
    	TextView titleView = new TextView(context);
    	titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	titleView.setText(episode.getName(), TextView.BufferType.SPANNABLE);
    	Spannable titleViewSpannable = (Spannable) titleView.getText();
    	titleViewSpannable.setSpan(new StyleSpan(Typeface.ITALIC), 0, titleViewSpannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    	textContainer.addView(titleView);
    	
    	//Relative layout parameters shared between objects
    	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    	params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    	
    	//Check box
    	if(hasAired)
    	{
    		//Loading animation
        	final LinearLayout animationContainer = new LinearLayout(context);
        	animationContainer.setPadding(0, 0, 8, 0);
        	animationContainer.setLayoutParams(params);
        	animationContainer.setVisibility(View.GONE);
        	final ImageView animationImage = new ImageView(context);
        	animationImage.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_popup_sync_1));
        	animationContainer.addView(animationImage);
        	container.addView(animationContainer);
    		
	    	CheckBox cb = new CheckBox(context);
	    	cb.setLayoutParams(params);
	    	cb.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.btn_check_label_background));
	    	cb.setButtonDrawable(context.getResources().getDrawable(R.drawable.btn_check_custom));
	    	cb.setChecked(episode.getSeen());
	    	container.addView(cb);
	    	
	    	//Events
	    	cb.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					final CheckBox cb = (CheckBox)v;
					
					//* hide check box
					cb.setVisibility(View.GONE);
					
					//* show + start loading animation
					animationContainer.setVisibility(View.VISIBLE);
					animationImage.startAnimation(AnimationUtils.loadAnimation(context, R.anim.rotate));
					
					//* call api
					Handler onCompleteHandler = new Handler(){
						@Override
				        public void handleMessage(Message msg) 
				        {
							//* stop + hide animation
							animationImage.setAnimation(null);
							animationContainer.setVisibility(View.GONE);
							
							//* show check box based on api call result
							cb.setVisibility(View.VISIBLE);
							cb.setChecked((Integer)msg.obj == 1);
				        }
					};
					SetSeenRunnable r = new SetSeenRunnable(episode, cb.isChecked(), onCompleteHandler);
					new Thread(r).start();
				}
			});
    	}

    	//Events
    	textContainer.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch(event.getAction())
				{
					case MotionEvent.ACTION_UP:
						AlertDialog alertDialog = new AlertDialog.Builder(context).create();
						alertDialog.setTitle(episode.getName());
						alertDialog.setMessage(episode.getSummary());
						alertDialog.setButton("Close", new DialogInterface.OnClickListener() {
						      public void onClick(DialogInterface dialog, int which) {
						    	  dialog.cancel();
						      }
						});
						
						alertDialog.show();
						return true;
					default:
						return true;
				}
			}
		});
    	
		return container;
	}
	
	public View createEpisodeCollectionItem(final Context context, final EpisodeCollection episodeCollection)
	{
		LinearLayout container = new LinearLayout(context);
		container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		container.setOrientation(LinearLayout.VERTICAL);
		container.setPadding(10, 5, 10, 5);

		TextView dateView = new TextView(context);
		dateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		dateView.setTextSize(20);
		dateView.setText(new SimpleDateFormat("EEE MMM dd").format(episodeCollection.getAirDate()), TextView.BufferType.SPANNABLE);
		Spannable dateViewSpannable = (Spannable) dateView.getText();
		dateViewSpannable.setSpan(new StyleSpan(Typeface.BOLD), 0, dateViewSpannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		Boolean hasAired = new Date().compareTo(episodeCollection.getAirDate()) > 0;
		dateView.setTextColor(hasAired ? Color.WHITE : context.getResources().getColor(R.color.pink));			
		
    	container.addView(dateView);
		
		ArrayList<Episode> episodes = episodeCollection.getEpisodes();
		for(int i = 0; i < episodes.size(); i++)
		{
			container.addView(createEpisodeItem(context, episodes.get(i), hasAired));
		}
		
		return container;
	}
}
